#pragma once
#include<stdint.h>
#include<stdio.h>
#include<string>
#include<sstream>
#include<iostream>
#include<string>
#include<cstring>
#include<regex>
using namespace std;

inline bool isMatch(char* string, char* wild)
{
	//cout << "string=" << string << endl;
	//cout << "wild=" << wild << endl;
	int len_str = strlen(string);
	int len_w = strlen(wild);

	/*if (len_w > len_str)
	{
		return false;
	}*/
	for (int i = 0; i <= len_w; i++)
	{
		if (wild[i] == '%')
		{
			if (wild[i+1]=='\0')
			{
				return true;
			}
			else
			{
				//匹配‘*’后面的字符,?与任何字符都可匹配
				int j = 0, k = 1, count = 1;
				for (j = i; j <= len_str; j++)
				{
					if (wild[i + 1] == string[j]) //找string中元素与wild[i + 1]相同的元素位置
					{
						break;
					}
					else if (wild[i + 1] == '_')
					{

						while (wild[i + 1 + k] == '_')
						{
							count++;	//统计‘？’的数量
							k++;
						}
						for (; j <= len_str; j++)
						{
							if (string[j] == wild[i + 1 + k])
							{
								break;
							}
						}
						j = j - count;	//刷新j的位置
						break;
					}
				}
				if (j < len_str)
				{
					return isMatch(string + j, wild + i + 1);		//递归处理*后面的部分
				}
				else
				{
					return false;
				}
			}		
			//return true;
		}
		else if (wild[i] == '_')
		{
			continue;
		}
		else if (wild[i] != string[i])
		{
			return false;
		}
		else
		{
			continue;
		}
	}
	return true;
}



inline int isMatch2(char *c1,char *c2){
	std::string s1=c1,s2=c2;
	// cout<<s1<<endl<<s2<<endl;
	std::string pattern = "^"+s2+"$";
	// std::string pattern = s2;
	int pos;
	while((pos = pattern.find("_")) != std::string::npos)   //替换所有指定子串
  {
      pattern.replace(pos, 1, ".");
  }
  while((pos = pattern.find("%")) != std::string::npos)   //替换所有指定子串
  {
      pattern = pattern.substr(0,pos)+".*"+pattern.substr(pos+1,pattern.size());
  }
  // std::cout<<pattern<<std::endl;
  std::regex reg(pattern,std::regex::icase);
  bool ret = regex_match(s1,reg);
  // cout<<(ret?"match":"not match")<<endl;

  // std::cout<< "like操作：" <<std::endl;
  // std::puts(c1);
  // std::puts(c2);
	return ret?0:-1;
}


inline int Getlenth(const char *str)
{
    int length = 0;
    if (str != NULL)
    {
        while (*str != '\0')
        {
            length++;
            str++;
        }
    }
    return length;
}

inline bool is_contains_dot(char *str)
{
    int len = Getlenth(str);
    bool t = false;
    for(int i = 0;i<len;i++){
        if(str[i]=='.'){
            t = true;
            break;
        }
    }
    return t;
} 

inline int string_to_int(char *str)
{
    if(  str[0]<'0'|| str[0]>'9' ){
        return 0;
    }
    char a[100];
    int len = Getlenth(str);
    int count = 0;
    for(int i = 0;i<len;i++){
        if((str[i]<='9' && str[i]>='0') || str[i] == '.'){
            a[count++] = str[i];
        }else{
            break;
        }
    }
    a[count++]= '\0';
    return stoi(a);
} 

inline float string_to_float(char *str)
{
    if(  str[0]<'0'|| str[0]>'9' ){
        return 0.0;
    }
    char a[100];
    int len = Getlenth(str);
    int count = 0;
    for(int i = 0;i<len;i++){
        if((str[i]<='9' && str[i]>='0') || str[i] == '.'){
            a[count++] = str[i];
        }else{
            break;
        }
    }
    a[count++]= '\0';
    return stof(a);
} 

inline int float_to_int(float f)
{
    int digit = (int)f;
    float back = f-digit;
    if(back>=0.5){
        return digit+1;
    }else{
        return digit;
    }
} 


inline void removeLastZero(char* numstr)
{
	if (NULL == strchr(numstr, '.'))
		return;
 
	int length = strlen(numstr);
	for (int i = length - 1; i > 0; --i)
	{
		if ('\0' == numstr[i])
		{
			continue;
		}
		else if ('0' == numstr[i])
		{
			numstr[i] = '\0';
		}
		else if ('.' == numstr[i])// 小数点之后全为零
		{
			numstr[i] = '\0';
			break;
		}
		else// 小数点后有非零数字
		{
			break;
		}
	}
}
